package com.baizhi.realm;
import com.baizhi.dao.AdminDao;
import com.baizhi.entity.Admin;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAccount;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by Administrator on 2019/4/19.
 */

/**
 * [main]
 MyRealm=com.baizhi.realm.MyRealm
 securityManager.realms=$MyRealm


 */



public class MyRealm extends AuthenticatingRealm {
    @Autowired
    private AdminDao adminDao;


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        1.从token中获取账号
        String username = (String) token.getPrincipal();
        Admin admin = adminDao.selectByName(username);

//        3.创建一个info 封装数据库查询出来的数据

//        如果账号不存在返回 null 就会抛异常
        if (admin!=null){
            /**
             * 三个参数
             * 账号
             * 密码
             * 当前对象的名字  this.getName()
             */
            SimpleAccount simpleAccount = new SimpleAccount(admin.getAdminUsername(),admin.getAdminPassword(), ByteSource.Util.bytes(admin.getSalt()),this.getName());
            return simpleAccount;
        }
        return null;
    }
}